package com.zhitan.ems.app.sip.controller;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.sip.entity.SipConfigEntity;
import com.zhitan.ems.app.sip.service.SipConfigService;
import com.zhitan.ems.app.sip.vo.SipConfigVO;
import com.zhitan.ems.app.sip.wrapper.SipConfigWrapper;
import com.zhitan.ems.common.annotation.Log;
import com.zhitan.ems.common.core.controller.BaseController;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.enums.BusinessType;
import com.zhitan.ems.common.utils.DateUtils;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.common.utils.poi.ExcelUtil;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * sip系统配置
 * 
 * @author zhitan
 * 
 * @date 2025-09-19 22:49:44
 */
@Api(tags = "sip系统配置", value = "sip系统配置")
@RestController
@RequestMapping("intra/sip/sipconfig")
public class SipConfigController extends BaseController {
	@Autowired
	private SipConfigService sipConfigService;

	/**
	 * 列表
	 */
	@ApiOperation(value = "列表", response = VPage.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "标题", paramType = "query", required = false, dataType = "String"), })
	@GetMapping("/browse")
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:list')")
	public R<VPage<SipConfigVO>> browse(@RequestParam Map<String, Object> params) {
		QueryWrapper<SipConfigEntity> queryWrapper = VQueryKit.getQueryWrapperByMap(params, Arrays.asList("name"),
				SipConfigEntity.class);
		queryWrapper.orderByDesc("id");
		queryWrapper.like(StringUtils.isNotNull(params.get("name")), "name", params.get("name"));
		IPage<SipConfigEntity> pages = sipConfigService.page(VQueryKit.getPage(params), queryWrapper);
		return R.data(SipConfigWrapper.build().toVPageVO(pages));
	}

	@GetMapping("/listStat")
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:list')")
	public R<Map<String, Object>> listStat(@RequestParam Map<String, Object> params) throws ParseException {
		QueryWrapper<SipConfigEntity> queryWrapper = VQueryKit.getQueryWrapperByMap(params, Arrays.asList("name"),
				SipConfigEntity.class);
		queryWrapper.select("count(id) as total,sum(id) as all");
		queryWrapper.like(StringUtils.isNotNull(params.get("name")), "name", params.get("name"));
		if (params.containsKey("createDate")) {
			queryWrapper.between("create_at",
					DateUtils.getStartOfDate(DateUtils.str_ToDate(params.get("createDate").toString())),
					DateUtils.getEndOfDate(DateUtils.str_ToDate(params.get("createDate").toString())));
		}
		Map<String, Object> stat = sipConfigService.getMap(queryWrapper);
		return R.data(stat);
	}

	@ApiOperation("导出")
	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:list')")
	@PostMapping("/toExport")
	public void toExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
		QueryWrapper<SipConfigEntity> queryWrapper = VQueryKit.getQueryWrapperByMap(params, Arrays.asList("name"),
				SipConfigEntity.class);
		queryWrapper.orderByDesc("id");
		queryWrapper.like(StringUtils.isNotNull(params.get("name")), "name", params.get("name"));
		List<SipConfigEntity> sipConfigList = sipConfigService.list(queryWrapper);
		ExcelUtil<SipConfigEntity> util = new ExcelUtil<>(SipConfigEntity.class);
		util.exportExcel(response, sipConfigList, "参数数据");
	}

	/**
	 * 信息
	 */
	@ApiOperation(value = "信息", response = SipConfigVO.class)
	@GetMapping("/detail/{id}")
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:info')")
	public R<SipConfigVO> detail(@PathVariable("id") Long id) {
		SipConfigEntity sipConfig = sipConfigService.getById(id);
		return R.data(SipConfigWrapper.build().entityVO(sipConfig));
	}

	/**
	 * 保存&更新
	 */
	@Log(title = "sip系统配置", businessType = BusinessType.ADDANDEDIT)
	@ApiOperation(value = "保存&更新", response = Boolean.class)
	@PostMapping("/submit")
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:save')")
	public R<SipConfigVO> submit(@Valid @RequestBody SipConfigVO sipConfig) {
		if (sipConfig.getId() == null) {
			return add(sipConfig);
		} else {
			return edit(sipConfig);
		}
	}

	@Log(title = "sip系统配置", businessType = BusinessType.INSERT)
	@ApiOperation(value = "新增", response = Boolean.class)
	@PostMapping("/add")
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:save')")
	public R<SipConfigVO> add(@Valid @RequestBody SipConfigVO sipConfig) {
		if (sipConfig.getId() == null) {
			sipConfig.setCreateAt(new Date());
			sipConfig.setIsDelete(0);
			sipConfig.setUpdateAt(new Date());
		}
		if (sipConfigService.save(sipConfig)) {
			return R.ok(sipConfig);
		}
		return R.error("数据错误");
	}

	@Log(title = "sip系统配置", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "更新", response = Boolean.class)
	@PostMapping("/edit")
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:update')")
	public R<SipConfigVO> edit(@Valid @RequestBody SipConfigVO sipConfig) {
		sipConfig.setUpdateAt(new Date());
		if (sipConfigService.updateById(sipConfig)) {
			return R.ok(sipConfig);
		}
		return R.error("数据错误");
	}

	/**
	 * 删除
	 */
	@ApiOperation(value = "删除", response = Boolean.class)
	@DeleteMapping("/delete")
	@PreAuthorize("@ss.hasPermi('sip:sipconfig:delete')")
	@Log(title = "sip系统配置", businessType = BusinessType.DELETE)
	public R<Boolean> delete(@RequestBody Long[] ids) {
		return R.ok(sipConfigService.removeByIds(Arrays.asList(ids)));
	}

}
